package com.ea.eadp.nexus.test.pageDefinition;

import com.ea.eadp.nexus.test.common.PageOperation;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

/**
 * Created with IntelliJ IDEA.
 * User: WenjieLu
 * Date: 4/10/13
 * Time: 12:09 PM
 * To change this template use File | Settings | File Templates.
 */
public class AccountPage_Web extends BasePage {

    public AccountPage_Web(WebDriver driver) {
        super(driver, AccountPage_Web.class);
    }

    public void clickAboutMeTab() throws Exception{
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@id='nav_aboutme']/div/div")).click();
        PageOperation.WebDriverWait(driver, "editSecuritySection");
        Thread.sleep(3000);
    }

    //click 'edit' link to open change security question window
    public void clickEdit() {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//a[@id='editSecuritySection2']")).click();
        PageOperation.WebDriverWait(driver, "challenge_securityanswer");
    }

    public void clickSecQuestionTab() {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//li[@id='securityQATab']/a/span")).click();
        PageOperation.WebDriverWait(driver, "questionContainerParent");
    }

    public String getCurrentSecQuesion() {
        driver.switchTo().window(this.operationWindowHandle);
        return driver.findElement(By.xpath("//dd[@id='secQuestion1']")).getText();
    }

    public void clickToSaveSecQuestions() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//a[@id='savebtn_sqa']/span")).click();
        Thread.sleep(2000);
    }

    public String getSecAnswerError() {
        return getElementStringByXPath("//div[@id='container_securityanswer']/div/span[3]");
    }

    public void setCurrentPwd(String currentPwd) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(currentPwd);
        PageOperation.SendKeyToElementAfterWait(driver, "originalPassword", currentPwd);
    }

    public void setNewPwd(String newPwd) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(newPwd);
        PageOperation.SendKeyToElementAfterWait(driver, "newPassword", newPwd);
    }

    public void setConfirmPwd(String confirmPwd) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(confirmPwd);
        PageOperation.SendKeyToElementAfterWait(driver, "newPasswordR", confirmPwd);
    }

    public void setCurrentSecAnswer(String currentAnswer){
        driver.switchTo().window(this.operationWindowHandle);
        log.info(currentAnswer);
        PageOperation.SendKeyToElementByPath(driver,
                "//input[@id='challenge_securityanswer']",
                currentAnswer);
        //PageOperation.SendKeyToElementAfterWait(driver, "challenge_securityanswer_for_password", currentAnswer);
    }

    public void clickToSaveNewPwd() throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//a[@id='savebtn_cpass']/span/span")).click();
        Thread.sleep(2000);
    }

    public void clickContinueToSaveSecQuestion() throws Exception{
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//a[@id='savebtn_security_question_challenge']/span")).click();
        PageOperation.WebDriverWait(driver,"passwordTab");
    }

    public void clickContinueToShowError() throws Exception{
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//a[@id='savebtn_security_question_challenge']/span")).click();
        PageOperation.WebDriverWaitByXpath(driver,"//div[@id='container_securityanswer']/div/span[3]");
    }

    public String getCurrentPwdError() {
        return getElementStringByXPath("//div[@id='container_form_originalpassword']/span[3]");
    }

    public void setSecurityQuestion(String cssSelector, String question) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(question);
        PageOperation.SelectCssDropDownItem(driver, cssSelector, question);
    }

    public void setSecurityAnswer(String elementID, String answer) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(answer);
        PageOperation.SendKeyToElementAfterWait(driver, elementID, answer);
    }

    public void setSecAnswer1(String answer) {
        setSecurityAnswer("securityAnswer1", answer);
    }

    public void clickAddSecQuestion(String newQuestion) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//a[@id='addMoreLink']")).click();
        PageOperation.WebDriverWait(driver, newQuestion);
    }

    public void clickRemoveSecQuesiton(int index) throws Exception {
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@id='questionBlock" + index + "']/div[3]/span/a/img")).click();
        //driver.findElement(By.xpath("//img[@id='RemoveQuestionCloseButton']")).click();
        Thread.sleep(2000);
    }

    public boolean elementIsExist(String xpath) {
        driver.switchTo().window(this.operationWindowHandle);
        try {
            WebElement element=driver.findElement(By.xpath(xpath));
            if(element.isDisplayed()==false)
                return false;
            //PageOperation.WebDriverWait(driver,"RemoveQuestionCloseButton");
        } catch (NoSuchElementException e) {
            return false;
        }
        return true;
    }

    public boolean addLinkIsExist() {
        driver.switchTo().window(this.operationWindowHandle);
        try {
            WebElement element = driver.findElement(By.xpath("//div[@id='addMoreLinkC']"));
            if (element.getAttribute("style").endsWith("none;"))
                return false;
            else
                return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    public boolean removeButtonIsExist(int index) {
        return elementIsExist("//div[@id='questionBlock" + index + "']/div[3]/span/a/img");
    }

    public boolean sendEmailLinkIsExist(){
        return elementIsExist("//a[@id='sendEmailLink']") ;
    }

    public void clickSecurityTab() throws Exception{
        driver.switchTo().window(this.operationWindowHandle);
        driver.findElement(By.xpath("//div[@id='nav_security']/div/div")).click();
        PageOperation.WebDriverWait(driver, "editSecuritySection2");
    }
}
